package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.proactive.webapp.dao.WorksheetFavoriteDAO;
import com.proactive.webapp.entity.WorksheetFavorite;

public class WorksheetFavoriteDAOImpl implements WorksheetFavoriteDAO {
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public void insert(WorksheetFavorite worksheetFavorite) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO worksheet_favorite(worksheetID, memberAccount, createDate) VALUES(?,?,NOW())";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetFavorite.getWid());
			smt.setString(2, worksheetFavorite.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public void delete(WorksheetFavorite worksheetFavorite) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM worksheet_favorite WHERE worksheetID=? AND memberAccount=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetFavorite.getWid());
			smt.setString(2, worksheetFavorite.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

}
